package com.jeeplus.modules.project.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.project.entity.BaseProjectInfo;
import com.jeeplus.modules.project.service.BaseProjectInfoService;

/**
 * 项目基本信息类Controller
 * @author jacky
 * @version 2017-12-19
 */
@Controller
@RequestMapping(value = "${adminPath}/project/baseProjectInfo")
public class BaseProjectInfoController extends BaseController {

	@Autowired
	private BaseProjectInfoService baseProjectInfoService;
	
	@ModelAttribute
	public BaseProjectInfo get(@RequestParam(required=false) String id) {
		BaseProjectInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = baseProjectInfoService.get(id);
		}
		if (entity == null){
			entity = new BaseProjectInfo();
		}
		return entity;
	}
	
	/**
	 * 项目基本信息列表页面
	 */
	@RequiresPermissions("project:baseProjectInfo:list")
	@RequestMapping(value = {"list", ""})
	public String list(BaseProjectInfo baseProjectInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		//baseProjectInfo.setStatus(1);
		Page<BaseProjectInfo> page = baseProjectInfoService.findPage(new Page<BaseProjectInfo>(request, response), baseProjectInfo); 
		model.addAttribute("page", page);
		return "modules/project/baseProjectInfoList";
	}
	
	/**
	 * 列表页面-课题论证
	 * 下面的几块都要根据状态来，全部填完了之后才能申请流程
	 */
	@RequiresPermissions("project:baseProjectInfo:courseCheck")
	@RequestMapping(value="courseCheckList")
	public String courseCheckList(BaseProjectInfo baseProjectInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		baseProjectInfo.setStatus(1);
		Page<BaseProjectInfo> page = baseProjectInfoService.findPage(new Page<BaseProjectInfo>(request, response), baseProjectInfo); 
		model.addAttribute("page", page);
		return "modules/project/course/basePtInfoCKList";
	}
	
	/**
	 * 列表页面-分工安排
	 */
	@RequiresPermissions("project:baseProjectInfo:arrange")
	@RequestMapping(value="arrangeList")
	public String arrangeList(BaseProjectInfo baseProjectInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		baseProjectInfo.setStatus(1);
		Page<BaseProjectInfo> page = baseProjectInfoService.findPage(new Page<BaseProjectInfo>(request, response), baseProjectInfo); 
		model.addAttribute("page", page);
		return "modules/project/arrange/basePtInfoArrgList";
	}
	
     /**
	 * 列表页面-经费预算
	 */
	@RequiresPermissions("project:baseProjectInfo:budget")
	@RequestMapping(value="budgetList")
	public String budgetList(BaseProjectInfo baseProjectInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		baseProjectInfo.setStatus(1);
		Page<BaseProjectInfo> page = baseProjectInfoService.findPage(new Page<BaseProjectInfo>(request, response), baseProjectInfo); 
		model.addAttribute("page", page);
		return "modules/project/budget/basePtInfoBudgetList";
	}
	
	/**
	 * 列表页面-立项列表
	 */
	@RequiresPermissions("project:baseProjectInfo:setup")
	@RequestMapping(value="setupList")
	public String setupList(BaseProjectInfo baseProjectInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		baseProjectInfo.setFlag(1);
		List<Integer> list = new ArrayList<Integer>();
		list.add(3);
		list.add(5);
		baseProjectInfo.setStatusArray(list);
		Page<BaseProjectInfo> page = baseProjectInfoService.findPage(new Page<BaseProjectInfo>(request, response), baseProjectInfo); 
		model.addAttribute("page", page);
		return "modules/project/setup/setUpList";
	}
	
	@RequiresPermissions("project:baseProjectInfo:list")
	@RequestMapping(value = "aysnList")
	@ResponseBody
	public List<BaseProjectInfo> aysnList(BaseProjectInfo baseProjectInfo){
		List<BaseProjectInfo> list = baseProjectInfoService.findList(baseProjectInfo);
		return list;
	}

	/**
	 * 查看，增加，编辑项目基本信息类表单页面
	 */
	@RequiresPermissions(value={"project:baseProjectInfo:view","project:baseProjectInfo:add","project:baseProjectInfo:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(BaseProjectInfo baseProjectInfo, Model model) {
		model.addAttribute("baseProjectInfo", baseProjectInfo);
		return "modules/project/baseProjectInfoForm";
	}
	
	/**
	 * 上传论证资料Form
	 */
	@RequiresPermissions(value={"project:baseProjectInfo:edit"},logical=Logical.OR)
	@RequestMapping(value = "courseCheckForm")
	public String courseCheckForm(BaseProjectInfo baseProjectInfo, Model model) {
		model.addAttribute("baseProjectInfo", baseProjectInfo);
		return "modules/project/course/basePjtCourseCheckForm";
	}
	
	/**
	 * 查看论证资料Form
	 */
	@RequestMapping(value = "viewCourseCheckForm")
	public String viewCourseCheckForm(BaseProjectInfo baseProjectInfo, Model model) {
		model.addAttribute("baseProjectInfo", baseProjectInfo);
		return "modules/project/course/courseCheckView";
	}
	
	/**
	 * 保存资料
	 * @param baseProjectInfo
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "saveCourseCheck")
	public String saveCourseCheck(BaseProjectInfo baseProjectInfo, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if(!StringUtils.isEmpty(baseProjectInfo.getFilePath())){
			baseProjectInfo.setIsUploadFile(1);
		}
		baseProjectInfoService.updateCourseCheckPath(baseProjectInfo);//保存
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/project/baseProjectInfo/courseCheckList";
	}

	/**
	 * 保存项目基本信息类
	 */
	@RequiresPermissions(value={"project:baseProjectInfo:add","project:baseProjectInfo:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(BaseProjectInfo baseProjectInfo, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, baseProjectInfo)){
			return form(baseProjectInfo, model);
		}
		if(!baseProjectInfo.getIsNewRecord()){//编辑表单保存
			BaseProjectInfo t = baseProjectInfoService.get(baseProjectInfo.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(baseProjectInfo, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			baseProjectInfoService.save(t);//保存
		}else{//新增表单保存
			baseProjectInfoService.save(baseProjectInfo);//保存
		}
		addMessage(redirectAttributes, "保存项目基本信息类成功");
		return "redirect:"+Global.getAdminPath()+"/project/baseProjectInfo/?repage";
	}
	
	/**
	 * 删除项目基本信息类
	 */
	@RequiresPermissions("project:baseProjectInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(BaseProjectInfo baseProjectInfo, RedirectAttributes redirectAttributes) {
		baseProjectInfoService.delete(baseProjectInfo);
		addMessage(redirectAttributes, "删除项目基本信息类成功");
		return "redirect:"+Global.getAdminPath()+"/project/baseProjectInfo/?repage";
	}
	
	/**
	 * 批量删除项目基本信息类
	 */
	@RequiresPermissions("project:baseProjectInfo:edit")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			baseProjectInfoService.delete(baseProjectInfoService.get(id));
		}
		addMessage(redirectAttributes, "删除项目基本信息类成功");
		return "redirect:"+Global.getAdminPath()+"/project/baseProjectInfo/?repage";
	}
	
	/**
	 * 跳转到项目选择页面
	 * @param baseProjectInfo
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "projectApproList")
	public String projectApproList(Model model,BaseProjectInfo baseProjectInfo,HttpServletRequest request, HttpServletResponse response) {
		baseProjectInfo.setStatus(1);
		Page<BaseProjectInfo> page = baseProjectInfoService.findPage(new Page<BaseProjectInfo>(request, response), baseProjectInfo); 
		model.addAttribute("page", page);
		return "modules/project/projectApply/baseProtInfoListStartPrcs";
	}
	
	/**
	 * 作废
	 * @param baseProjectInfo
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions(value="project:baseProjectInfo:edit")
	@RequestMapping(value = "rubbish")
	@ResponseBody
	public Map<String,Object> rubbish(BaseProjectInfo baseProjectInfo, Model model, RedirectAttributes redirectAttributes){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			baseProjectInfo.setStatus(0);
			baseProjectInfoService.updateStatus(baseProjectInfo);
			result.put("code", 0);
			result.put("msg", "作废成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -1);
			result.put("msg", "作废失败");
		}
		return result;
	}
	/**
	 * 给项目立项
	 * @param baseProjectInfo
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions(value="project:baseProjectInfo:setup")
	@RequestMapping(value = "toSetup")
	@ResponseBody
	public Map<String,Object> setup(BaseProjectInfo baseProjectInfo, Model model, RedirectAttributes redirectAttributes){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			baseProjectInfo.setStatus(5);
			baseProjectInfo.setSetupTime(new Date()); //立项时间
			baseProjectInfoService.updateStatus(baseProjectInfo);
			result.put("code", 0);
			result.put("msg", "立项成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -1);
			result.put("msg", "立项失败");
		}
		return result;
	}
	

}